package com.mryt.service;

import com.google.common.collect.Maps;
import com.mryt.bean.GenConf;
import com.mryt.bean.Table;
import com.mryt.util.FreemarkerUtil;
import com.mryt.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.util.List;
import java.util.Map;

@Service
public class CodeGenService {
	private static final Logger logger = LoggerFactory.getLogger(CodeGenService.class);
	@Resource
	private TableService tableService;
	@Resource
	private GenConf genConf;

	public void genCode() throws Exception {
		logger.info("gen code,genConf={}", genConf);

		if (StrUtil.isBlank(genConf.getOutputDir())) {

			logger.error("output dir can NOT be null");
			return;
		}
		File outputDir = new File(genConf.getOutputDir());
		if (!outputDir.exists()) {
			outputDir.mkdirs();
		} else {
			outputDir.delete();
			outputDir.mkdirs();
		}

		File baseDbBeanPackage = new File(genConf.getOutputDir() + "/" + "/domain/");
		File baseMapperPackage = new File(genConf.getOutputDir() + "/dao/");
		File baseMapperXmlPacakge = new File(genConf.getOutputDir() + "/dao/");
		File controllerPacakge = new File(genConf.getOutputDir() + "/controller/");
		File servicePacakge = new File(genConf.getOutputDir() + "/service/");
		File serviceImplPacakge = new File(genConf.getOutputDir() + "/serviceImpl/");
		File testPacakge = new File(genConf.getOutputDir() + "/test/");

		
		baseDbBeanPackage.mkdirs();
		baseMapperPackage.mkdirs();
		baseMapperXmlPacakge.mkdirs();
		controllerPacakge.mkdirs();
		servicePacakge.mkdirs();
		serviceImplPacakge.mkdirs();
		testPacakge.mkdirs();

		List<Table> tableList = tableService.getTableInfoList();
		int i = 0;
		for (Table table : tableList) {
			logger.info("i --> {}",i);
			i++;
			String tableIgnored = genConf.getTableIgnored();
			if(tableIgnored.contains(table.getTableName())){
				continue;
			}
			logger.info("gen code for table,tableName={}", table.getTableName());
			Map<String, Object> rootMap = Maps.newHashMap();
			rootMap.put("table", table);
			rootMap.put("cfg", genConf);

			File baseBeanTemplate = ResourceUtils.getFile("classpath:templates/ssi/bean.ftl");
			File baseMapperTemplate = ResourceUtils.getFile("classpath:templates/ssi/base_mapper.ftl");
			File baseMapperXmlTemplate = ResourceUtils.getFile("classpath:templates/ssi/base_mapper_xml.ftl");
			File serviceTemplate = ResourceUtils.getFile("classpath:templates/ssi/service.ftl");
			File serviceImplTemplate = ResourceUtils.getFile("classpath:templates/ssi/serviceImpl.ftl");
			File controllerTemplate = ResourceUtils.getFile("classpath:templates/ssi/controller.ftl");
			File testTemplate = ResourceUtils.getFile("classpath:templates/ssi/test.ftl");
//
			FileWriter baseBeanWriter = new FileWriter(baseDbBeanPackage.getAbsolutePath() + "/" + table.getBeanName() + ".java");
			FileWriter baseMapperWriter = new FileWriter(baseMapperPackage.getAbsolutePath() + "/" + table.getBeanName() + "DAO.java");
			FileWriter baseMapperXmlWriter = new FileWriter(baseMapperXmlPacakge.getAbsolutePath() + "/" + table.getBeanName() + "DAO.xml");
			FileWriter controllerWriter = new FileWriter(controllerPacakge.getAbsolutePath() + "/" + table.getBeanName() + "Controller.java");
			FileWriter serviceWriter = new FileWriter(servicePacakge.getAbsolutePath() + "/" + table.getBeanName() + "Service.java");
			FileWriter serviceImplWriter = new FileWriter(serviceImplPacakge.getAbsolutePath() + "/" + table.getBeanName() + "ServiceImpl.java");
			FileWriter testWriter = new FileWriter(testPacakge.getAbsolutePath() + "/" + table.getBeanName() + "ServiceImplTest.java");

			FreemarkerUtil.flushData(baseBeanTemplate.getAbsolutePath(), baseBeanWriter, rootMap);
			FreemarkerUtil.flushData(baseMapperTemplate.getAbsolutePath(), baseMapperWriter, rootMap);
			FreemarkerUtil.flushData(baseMapperXmlTemplate.getAbsolutePath(), baseMapperXmlWriter, rootMap);
			FreemarkerUtil.flushData(serviceTemplate.getAbsolutePath(), serviceWriter, rootMap);
			FreemarkerUtil.flushData(serviceImplTemplate.getAbsolutePath(), serviceImplWriter, rootMap);
			FreemarkerUtil.flushData(controllerTemplate.getAbsolutePath(), controllerWriter, rootMap);
			FreemarkerUtil.flushData(testTemplate.getAbsolutePath(), testWriter, rootMap);
		}
		
		dumpTableExcel(tableList);
		
	}
	
	
	
	private void dumpTableExcel(List<Table> tableList){
		
	}

}
